<template>
  <div class="login">
    <!-- header -->
    <s-header :name="type === 'login' ? '登录' : '注册'"></s-header>
    <img
      class="logo"
      src="//s.yezgea02.com/1604045825972/newbee-mall-vue3-app-logo.png"
      alt=""
    />
    <!-- 登录 -->
    <div v-if="type === 'login'" class="login login-body">
      <van-form>
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="verify"
          name="验证码"
          label="验证码"
          placeholder="请输入验证码"
        >
          <template #button>
            <vue-img-verify ref="verifyRef"/>
          </template>
        </van-field>

        <div style="margin: 16px">
          <div class="link-register" @click="toggle('register')">立即注册</div>
          <van-button round block type="primary" native-type="submit" @click="onSubmit">
            登录
          </van-button>
        </div>
      </van-form>
    </div>

    <!-- 注册 -->
    <div v-else class="login-body register">
      <van-form>
        <van-field
          v-model="username1"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password1"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="verify"
          name="验证码"
          label="验证码"
          placeholder="请输入验证码"
        >
          <template #button>
            <vue-img-verify ref="verifyRef"/>
          </template>
        </van-field>

        <div style="margin: 16px">
          <div class="link-register" @click="toggle('login')">已有登录账号</div>
          <van-button round block type="primary" native-type="submit" @click="onSubmit">
            注册
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import sHeader from "@/components/SimpleHeader.vue";
import vueImgVerify from "@/components/VueImgVerify.vue";
import { reactive, toRefs,ref } from "vue";
import {register,login} from '@/service/user';
import { Toast } from 'vant';
import md5 from 'js-md5';
import { useRouter } from 'vue-router';

export default {
  setup() {
    const router = useRouter()
    const verifyRef = ref(null)
    const state = reactive({
      username: "",
      username1:"",
      password: "",
      password1:"",
      verify: "",
      type:'login'
    });
    const toggle = (type) => {
        state.type = type
    }
    // 登录注册
    const onSubmit = async () => {
      if(state.verify.toLowerCase() !== verifyRef.value.imgCode.toLowerCase()) {
        Toast.fail('验证码有误')
        return
      }
      console.log(verifyRef.value.imgCode)
      if(state.type == 'login') {
        const {data} = await login({
          'loginName':state.username,
          'passwordMd5':md5(state.password),
        })
        localStorage.setItem('token',data)
        // console.log(data)
        router.push({path:'/home'})
      } else {
        await register({
          'loginName':state.username1,
          'password': state.password1

        })
        Toast.success('注册成功')
        state.type = 'login'
      }
    }
    return {
      ...toRefs(state),
      toggle,
      onSubmit,
      verifyRef
    };
  },
  components: {
    sHeader,
    vueImgVerify,
  },
};
</script>

<style lang="less">
.login {
  .logo {
    width: 120px;
    height: 120px;
    display: block;
    margin: 80px auto 20px;
  }
  .login-body {
    padding: 0 20px;
  }
  .login {
    .link-register {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: inline-block;
    }
  }
  .register {
    .link-login {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: inline-block;
    }
  }
  .verify-bar-area {
    margin-top: 24px;
    .verify-left-bar {
      border-color: #1baeae;
    }
    .verify-move-block {
      background-color: #1baeae;
      color: #fff;
    }
  }
  .verify {
    > div {
      width: 100%;
    }
    display: flex;
    justify-content: center;
    .cerify-code-panel {
      margin-top: 16px;
    }
    .verify-code {
      width: 40% !important;
      float: left !important;
    }
    .verify-code-area {
      float: left !important;
      width: 54% !important;
      margin-left: 14px !important;
      .varify-input-code {
        width: 90px;
        height: 38px !important;
        border: 1px solid #e9e9e9;
        padding-left: 10px;
        font-size: 16px;
      }
      .verify-change-area {
        line-height: 44px;
      }
    }
  }
}
</style>